package org.item.user.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.item.common.bean.SysMenu;
import org.item.common.bean.SysPermission;
import org.item.common.bean.SysRole;
import org.item.common.bean.SysUser;
import org.item.common.enumbean.UserStatus;
import org.item.common.service.IUserService;
import org.item.user.mapper.MenuMapper;
import org.item.user.mapper.RoleMapper;
import org.item.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Service
@DubboService
public class UserService extends ServiceImpl<UserMapper,SysUser> implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    MenuMapper menuMapper;
    @Override
    public SysUser insert(SysUser user) {
        super.save(user);
        int id=user.getId();
        SysUser result=this.getById(id);
        return result;
    }

    @Override
    public SysUser update(SysUser user) {
        super.updateById(user);
        int id=user.getId();
        SysUser result=this.getById(id);
        return result;
    }

    @Override
    public int delete(Integer id) {
        super.removeById(id);
        return 1;
    }

    @Override
    public SysUser getById(Integer id) {
      return super.getById(id);
    }

    @Override
    public IPage<SysUser> getListPage(IPage page, SysUser user) {
        QueryWrapper wrapper=new QueryWrapper(user);
        return super.page(page,wrapper);
    }

    @Override
    public List<SysUser> getList(SysUser user) {
        QueryWrapper wrapper=new QueryWrapper(user);
        return super.list(wrapper);
    }

    @Override
    public int setStatus(Integer id) {
      SysUser user=super.getById(id);
      UserStatus status=user.getStatus();
      if (status==UserStatus.FORBIDDEN){
          user.setStatus(UserStatus.NORMAL);
      }else {
          user.setStatus(UserStatus.FORBIDDEN);
      }
      super.getById(id);
      return 1;
    }

    @Override
    public SysUser assignRole(SysUser user) {
        int userid=user.getId();

        //先清空该用户原有的role
        super.getBaseMapper().deleteUserRoleByUserId(user.getId());
        List<SysRole> roles=user.getRoles();
        for(SysRole role:roles){
            super.getBaseMapper().insertUserRole(userid,role.getId());
        }

        return super.getById(userid);
    }



    @Override
    public SysUser getByUsername(String username) {
        SysUser sysUser = this.getBaseMapper().getOneByUsername(username);
        Integer userid = sysUser.getId();
        List<SysRole> roles = super.getBaseMapper().getUserRoles(userid);
        sysUser.setRoles(roles);
        List<SysPermission> permissions = null;
        for (SysRole role : roles){
            permissions = roleMapper.getPermissionsByRoleId(role.getId());
            role.setPermissions(permissions);
        }
        // 4. 根据权限查询菜单（去重）
        Set<SysMenu> uniqueMenus = new LinkedHashSet<>(); // 保持插入顺序
        if (permissions != null) {
            for (SysPermission permission : permissions) {
                if (permission.getMeanId() != null) {
                    SysMenu menu = menuMapper.getMeanByPermId(permission.getMeanId());
                    if (menu != null) {
                        uniqueMenus.add(menu); // 依赖SysMenu的equals和hashCode去重
                    }
                }
            }
            sysUser.setMenus(new ArrayList<>(uniqueMenus));
        }
        return sysUser;
    }
}
